package org.neurone.session.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Random;

import org.neurone.session.SessionHive;
import org.neurone.session.SessionServer;
import org.neurone.userspace.verification.Verification;

public class SessionHiveLocal implements SessionHive {
	protected SessionServer sessionServer;
	protected Verification verification;
	//用户ID与sessionKey绑定
	private static final HashMap<String, String> userKeyList= new HashMap<String, String>();
	protected static final HashMap<String, String> tokenUserList = new HashMap<String, String>();


	public SessionServer getSessionServer() {
		return sessionServer;
	}

	public Verification getVerification() {
		return verification;
	}

	public void setSessionServer(SessionServer sessionServer) {
		this.sessionServer = sessionServer;
	}

	public void setVerification(Verification verification) {
		this.verification = verification;
	}

	public SessionHiveLocal(SessionServer sessionServer, Verification verification)
	{	
		this.sessionServer = sessionServer;
		this.verification = verification;
	}

	public SessionHiveLocal()
	{

	}

	@Override
	public String getNewToken() {
		String nowUserID = sessionServer.getSessionUserID();
		if(nowUserID != null)
		{
			String newToken = createNewToken();
			tokenUserList.put(newToken, nowUserID);
			return newToken;			
		}
		return null;
	}

	@Override
	public String getNowSessionKey() {
		return sessionServer.getSessionKey();
	}

	@Override
	public String loginByToken(String onceToken, String appKey) {
		if(tokenUserList.containsKey(onceToken))
		{

			//获取token对应的用户ID
			String userId = tokenUserList.get(onceToken);

			//使用appKey_userId创建会话 ，建立app请求者到服务器的连接
			this.sessionServer.registerSession(appKey+"_"+userId, userId);
			String sessionKey = createSessionKey(this.sessionServer.getSessionID(), userId);
			this.sessionServer.registerSessionKey(sessionKey);

			tokenUserList.remove(onceToken);
			return sessionKey;
		}

		tokenUserList.remove(onceToken);
		return null;
	}

	@Override
	public void logoutSession() {
		userKeyList.remove(sessionServer.getSessionUserID());
		sessionServer.logoutSession();
	}

	@Override
	public boolean loginSession(String userId, String passwd) {
		
		if (verification!=null&&verification.verify(userId, passwd))
		{	
			System.out.print("loginSession");
			sessionServer.registerSession(userId, userId);
			String sessionKey = createSessionKey(this.sessionServer.getSessionID(), userId);
			this.sessionServer.registerSessionKey(sessionKey);
			return true;
		}

		return false;
	}

	@Override
	public String checkSession() {
		return sessionServer.getSessionUserID();
	}


	@Override
	public String getSessionKey() {
		return this.sessionServer.getSessionKey();
	}

	public String createNewToken() {
		return String.valueOf(new Date().getTime())+new Random().nextInt(10000);
	}

	public String createSessionKey(String sessionID, String userID) {
		return sessionID + "000" +userID;
	}

	@Override
	public String checkSessionKey(String sessionKey) {

		if ((sessionKey==null?"":sessionKey).equals(sessionServer.getSessionKey()))
		{
			return sessionServer.getSessionUserID();
		}
		return null;
	}

	@Override
	public String checkSessionKeyWithUser(String sessionKey, String userId) {
		//sessionKey和用户名是否全部有效才表示此会话连接有效
		if( (userId==null?"":userId).equals(this.sessionServer.getSessionUserID())
				&&checkSessionKey(sessionKey) != null)
		{
			return sessionServer.getSessionUserID();
		}
		return null;

	}	
}
